package cn.xorange.commons.security.service;

import cn.xorange.commons.security.constants.SecurityCacheKeys;
import cn.xorange.commons.security.entity.ClaimsInfo;
import cn.xorange.commons.security.entity.SecurityUser;
import cn.xorange.commons.security.utils.SecurityUtils;
import cn.xorange.commons.utils.cache.ICacheService;
import cn.xorange.commons.utils.lang.ObjectUtils;
import com.alibaba.fastjson2.JSON;

import javax.annotation.Resource;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * @author : yangjian
 * date : 2024/1/11
 * description :
 */
public class SecurityService {

    @Resource
    ICacheService cacheService;
    @Resource
    LoginHandlerFactory loginHandlerFactory;

    public SecurityUser getLoginUser() {
        String tokenKey = SecurityUtils.getTokenKey();
        return cacheService.getCacheObject(tokenKey);
    }

    public void refreshUserInfoCache(String userJsonStr){
        SecurityUser jsonUser = JSON.parseObject(userJsonStr, SecurityUser.class);
        if(Objects.nonNull(jsonUser)){
            ClaimsInfo claimsInfo = SecurityUtils.getClaimsInfo();
            if(Objects.nonNull(claimsInfo)) {
                String tokenKey = claimsInfo.tokenKey();
                String sysType = claimsInfo.getSysType();
                String loginType = claimsInfo.getLoginType();

                SecurityUser user = cacheService.getCacheObject(tokenKey);
                if (Objects.nonNull(jsonUser.getStaffId())) {
                    user.setStaffId(jsonUser.getStaffId());
                }
                if (Objects.nonNull(jsonUser.getAvatar())) {
                    user.setAvatar(jsonUser.getAvatar());
                }
                if (Objects.nonNull(jsonUser.getOrangeId())){
                    user.setOrangeId(jsonUser.getOrangeId());
                }
                if (Objects.nonNull(jsonUser.getSex())) {
                    user.setSex(jsonUser.getSex());
                }
                if (Objects.nonNull(jsonUser.getNickname())) {
                    user.setNickname(jsonUser.getNickname());
                }
                if (Objects.nonNull(jsonUser.getUsername())) {
                    user.setUsername(jsonUser.getUsername());
                }
                if (Objects.nonNull(jsonUser.getPhone())) {
                    user.setPhone(jsonUser.getPhone());
                }
                if (Objects.nonNull(jsonUser.getTenantId())) {
                    user.setTenantId(jsonUser.getTenantId());
                }
                if (Objects.nonNull(jsonUser.getCity())) {
                    user.setCity(jsonUser.getCity());
                }
                if(Objects.nonNull(jsonUser.getBirthday())){
                    user.setBirthday(jsonUser.getBirthday());
                }
                if(Objects.nonNull(jsonUser.getStar())){
                    user.setStar(jsonUser.getStar());
                }
                if(Objects.nonNull(jsonUser.getZodiac())){
                    user.setZodiac(jsonUser.getZodiac());
                }

                LoginHandler loginHandler = loginHandlerFactory.getLoginHandler(sysType, loginType);
                cacheService.setCacheObject(tokenKey, user, loginHandler.config().getTokenExpire(), TimeUnit.MINUTES);
            }

        }

    }

    public void setLoginErr(String source, String loginName,Long clockTime){
        String errKey = SecurityCacheKeys.loginErrKey(source, loginName);
        int num = ObjectUtils.toInt(cacheService.getCacheObject(errKey));
        num = num+1;
        cacheService.setCacheObject(errKey,num,clockTime, TimeUnit.SECONDS);
    }

    public Integer getLoginErr(String source, String loginName){
        String errKey = SecurityCacheKeys.loginErrKey(source, loginName);
        return ObjectUtils.toInt(cacheService.getCacheObject(errKey));
    }

    public void delLoginErr(String source, String loginName){
        String errKey = SecurityCacheKeys.loginErrKey(source, loginName);
        cacheService.delete(errKey);
    }


}
